﻿Public Partial Class Group_Popup
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Dim lbUser As Label = CType(Master.FindControl("lbMenu"), Label)
            'lbUser.Text = "Product Group"

            If Not (Session("GroupPopup") Is Nothing) Then
                ViewState("Group") = Session("GroupPopup").ToString().Split("$")(0)
                txtGroup.Text = Session("GroupPopup").ToString().Split("$")(1)
                txtTime.Text = Session("GroupPopup").ToString().Split("$")(3)
                If Not (radioPort.Items.FindByValue(Session("GroupPopup").ToString().Split("$")(2)) Is Nothing) Then
                    radioPort.SelectedValue = Session("GroupPopup").ToString().Split("$")(2)
                End If
            Else
                ViewState("Group") = ""
            End If
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Session("GroupPopup") = Nothing
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub
    Private Function check() As Boolean
        If (txtGroup.Text.Trim() = "") Then
            Return False
        End If
        If (radioPort.SelectedValue = "") Then
            Return False
        End If
        Return True
    End Function

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (check()) Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            Dim Group_no As String = ""
            If ViewState("Group").ToString() = "" Then
                Group_no = clsDB.GeneratorID(conn, "Product_Group")
            Else
                Group_no = ViewState("Group").ToString()
            End If

            '  tran = clsDB.BeginTran(conn)
            If (clsFc.InsertProduct_Group(ViewState("Group").ToString(), Group_no, txtGroup.Text.Trim(), radioPort.SelectedValue, txtTime.Text, conn, tran) > 0) Then
                Session("GroupPopup") = Nothing
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "parent.closeModal();alert('บันทึกข้อมูลเสร็จสมบูรณ์ !!');", True)
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุข้อมูลให้ครบ !!');", True)
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class